//
//  VideoViewController.m
//  Channel
//
//  Created by CPX on 2018/9/5.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoDetailController.h"

/**
 视频容器页
 @author yzl
 */
@interface VideoViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *recommendBtn;
@property (nonatomic, strong) UIButton *popularBtn;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) VideoDetailController *popularCtr;
@property (nonatomic, strong) VideoDetailController *recommendCtr;

@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initViews{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth*2, kScreenHeight);
    self.scrollView.backgroundColor = UIColorHex(#F6F6F6);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    self.recommendCtr = [VideoDetailController new];
    self.recommendCtr.isRecommend = YES;
    [self.scrollView addSubview:self.recommendCtr.view];
    [self addChildViewController:self.recommendCtr];
    
//    self.popularCtr = [VideoDetailController new];
//    [self.scrollView addSubview:self.popularCtr.view];
//    [self addChildViewController:self.popularCtr];
    
    if ([UIView appearance].semanticContentAttribute ==  UISemanticContentAttributeForceRightToLeft) {
//        self.popularCtr.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.recommendCtr.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
        [self.scrollView scrollToRight];
    }
    else{
        self.recommendCtr.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
//        self.popularCtr.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
    }
}

- (void)initNavigation{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, NavHeight)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    self.recommendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.recommendBtn addTarget:self action:@selector(onRecommendTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.recommendBtn setTitle:TLString(@"social_recommend") forState:UIControlStateNormal];
    [self.recommendBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
    self.recommendBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [view addSubview:self.recommendBtn];
    [self.recommendBtn sizeToFit];
    
    self.popularBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.popularBtn addTarget:self action:@selector(onPopularTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.popularBtn setTitle:TLString(@"social_popular") forState:UIControlStateNormal];
    [self.popularBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
    self.popularBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [view addSubview:self.popularBtn];
    [self.popularBtn sizeToFit];
    
    self.line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 3)];
    self.line.backgroundColor = UIColorHex(#00732F);
    [view addSubview:self.line];
    
    self.recommendBtn.bottom = NavHeight - 8.5;
    self.popularBtn.bottom = NavHeight - 8.5;
    self.line.bottom = NavHeight - 8.5;
    
    CGFloat offset = 65;
    CGFloat x = view.width/2;
    if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
        self.popularBtn.centerX = x-offset;
        self.recommendBtn.centerX = x+offset;
    }
    else{
        self.recommendBtn.centerX = x-offset;
        self.popularBtn.centerX = x+offset;
    }
    self.line.centerX = self.recommendBtn.centerX;
}

#pragma mark - actions

- (void)onRecommendTouched{
    
    [self animatedLine:YES];
    [self.recommendBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
    [self.popularBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
    [self.scrollView scrollRectToVisible:self.recommendCtr.view.frame animated:YES];
    [self logEvent:@"cha_click_popular"];
}

- (void)onPopularTouched{
    [self animatedLine:NO];
    [self.recommendBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
    [self.popularBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
    [self.scrollView scrollRectToVisible:self.popularCtr.view.frame animated:YES];
    [self logEvent:@"cha_click_recommend"];
}

- (void)animatedLine:(BOOL)isRecommend{
    CGRect frame = self.line.frame;
    if (isRecommend) {
        self.line.centerX = self.recommendBtn.centerX;
    }else{
        self.line.centerX = self.popularBtn.centerX;
        self.popularCtr.view.hidden = NO;
    }
    
    CGRect frame2 = self.line.frame;
    self.line.frame = frame;
    
    [UIView animateWithDuration:.2 animations:^{
        self.line.frame = frame2;
    } completion:^(BOOL finished) {
        if (finished) {
            if (isRecommend) {
                [self.recommendBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
                [self.popularBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
            }
            else{
                [self.recommendBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
                [self.popularBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
            }
        }
    }];
}

#pragma mark - scroll view

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0 ) {
        [self animatedLine:self.recommendBtn.centerX<self.popularBtn.centerX];
    }
    else if(scrollView.contentOffset.x == kScreenWidth){
        [self animatedLine:self.recommendBtn.centerX>self.popularBtn.centerX];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
}

#pragma mark - lazy load

- (VideoDetailController*)popularCtr{
    if (!_popularCtr) {
        _popularCtr = [VideoDetailController new];
        [self.scrollView addSubview:_popularCtr.view];
        [self addChildViewController:_popularCtr];
        if ([UIView appearance].semanticContentAttribute ==  UISemanticContentAttributeForceRightToLeft) {
            _popularCtr.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        }
        else{
            _popularCtr.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
        }
    }
    return _popularCtr;
}

@end
